<template>
  <div class="dialog">
    <el-dialog
      :title="titleText"
      :visible.sync="showDialog"
      width="45%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="原料名称:" prop="materialNo" class="w100p">
              <el-select v-model="formData.materialNo" filterable placeholder="请输入添加原料的名称">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import QualityManage from "@/api/processing-management/quarity-manage";
export default {
  name: "",
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    options: {
      type: Array,
      default: () => []
    },
    unitOptions: {
      type: Array,
      default: () => []
    },
    formDataCopy: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      formData: {},
      showDialog: this.show,
      pictureList: [],
      rules: {
        materialTypeNo: [
          {
            required: true,
            message: "请输入添加原料的名称",
            trigger: ["blur", "change"]
          }
        ],
        materialspec: [
          {
            required: true,
            message: "请输入产品规格",
            trigger: ["blur", "change"]
          }
        ],
        materialName: [
          {
            required: true,
            message: "请输入产品名称",
            trigger: ["blur", "change"]
          }
        ],
        unitNo: [
          {
            required: true,
            message: "请选择名称",
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
      this.pictureList = [];
    },
    formDataCopy(v) {
      this.formData = v;
    }
  },
  created() {},
  mounted() {},
  methods: {
    // 删除列表图片
    removePicture(file) {
      const index = this.pictureList.findIndex(item => {
        return item.uid === file.uid;
      });
      this.pictureList.splice(index, 1);
    },
    addPicture(file) {
      this.pictureList.push(file.file);
    },
    save(formData) {
      this.$refs[formData].validate(async valid => {
        if (valid) {
          // if (this.pictureList.length > 0) {
          //   let form = new FormData();
          //   form.append("file", this.pictureList[0]);
          //   let res = await QualityManage.addPicture(form);
          //   if (res.code !== 1) {
          //     this.$message.error("图片保存失败");
          //     return;
          //   }
          //   this.formData.pictureNo = res.data;
          // }
          this.$emit("addEvent", this.formData);
          this.close();
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("closeDialog");
      this.formData = {};
    }
  }
};
</script>

  <style lang="scss" scoped>
::v-deep
  .el-dialog__wrapper
  .el-dialog
  .el-dialog__body
  .el-form
  .el-form-item
  .el-form-item__label {
  min-width: 160px !important;
}
</style>
